/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Orders;
import com.yuhanginfo.modules.sys.entity.Store;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.service.OrdersChangeHistoryService;
import com.yuhanginfo.modules.sys.service.StoreService;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 订单变更记录Controller
 * @author yinhao
 * @version 2017-10-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/orderschangehistory")
public class OrdersChangeHistoryController extends BaseController {

	@Autowired
	private OrdersChangeHistoryService ordersChangeHistoryService;
	
	@Autowired
	private StoreService storeService;
	
	@ModelAttribute
	public Orders get(@RequestParam(required=false) String id) {
		Orders entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ordersChangeHistoryService.get(id);
		}
		if (entity == null){
			entity = new Orders();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:orderschangehistory:view")
	@RequestMapping(value = {"list", ""})
	public String list(Orders orders, HttpServletRequest request, HttpServletResponse response, Model model) {
		User us = UserUtils.getUser();
		orders.setSysUserId(us.getId());//此处查询的不是业务员id,而是管理员id
		Page<Orders> page = ordersChangeHistoryService.findPage(new Page<Orders>(request, response), orders); 
		List<Store> storeList=storeService.findStoreBySysUser(us);
		model.addAttribute("store", storeList);
		model.addAttribute("page", page);
		return "modules/sys/order/orderchangehistory/orderchangehistoryList";
	}
	
	@RequiresPermissions("sys:orderschangehistory:view")
	@RequestMapping(value = "form")
	@ResponseBody
	public Map<String,Object> form(Orders orders) {
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.putAll(ordersChangeHistoryService.findDetailList(orders));
		resultMap.put("resultCode", 1);
		return resultMap;
	}

}